package com.linln.quartz;

import com.linln.common.constant.NoticeTypeConst;
import com.linln.devtools.generate.utils.jAngel.utils.StringUtil;
import com.linln.modules.message.domain.MessageInfo;
import com.linln.modules.message.domain.MessageRefund;
import com.linln.modules.message.repository.MessageRefundRepository;
import com.linln.modules.message.service.MessageRefundService;
import com.linln.modules.message.service.MessageTopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.linln.modules.member.service.MemberAgentService;
import com.linln.modules.member.service.MemberDayCountService;
import com.linln.modules.member.service.MemberInfoService;
import com.linln.modules.member.service.MemberOrderService;
import com.linln.modules.member.service.MemberPayDayCountService;
import com.linln.modules.message.service.MessageDayCountService;
import com.linln.modules.message.service.MessageInfoService;
import com.linln.modules.store.service.StoreDayCountService;
import com.linln.modules.store.service.StoreinfoService;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@EnableAsync //并发设置
public class ScheduleJobInitListener {
        
    @Autowired
    private MemberDayCountService memberService;
    @Autowired
    private MessageDayCountService messageService;
    @Autowired
    private StoreDayCountService storeService;

    @Autowired
    private MemberPayDayCountService payService;

    @Autowired
	private MessageTopService messageTopServiceImpl;

    @Autowired
	private MessageRefundService messageRefundServiceImpl;

	    /**
	           * 获取用户数量
	     */
        @Async
		@Scheduled(cron = "0 5 1 * * ?")
	    //0 5 1 * * ? 每天凌晨1点05执行
	    public void getUserscheduled(){
        	memberService.insert();
	        log.info("=====>>>>>getUserscheduledcron  {}",System.currentTimeMillis());
	    }
        /**
                 * 获取信息数量
       */
  
        @Async//并发设置
		@Scheduled(cron = "0 5 1 * * ?")
	    public void getMessagescheduled() {
        	messageService.insert();
	        log.info("=====>>>>>使用getMessagescheduled{}", System.currentTimeMillis());
	    }
        /**
                    * 获取商户数量
         */
        @Async//并发设置
		@Scheduled(cron = "0 5 1 * * ?")
	    public void getStorescheduled() {
        	storeService.insert();
	        log.info("=====>>>>>getStorescheduled{}",System.currentTimeMillis());
	    }
        /**
         	* 获取金额数量
         */
		@Async//并发设置
		@Scheduled(cron = "0 5 1 * * ?")
		public void getMoneyscheduled() {
		 payService.insert();
		 log.info("=====>>>>>getMoneyscheduled{}",System.currentTimeMillis());
		}

		@Async//并发设置
		@Scheduled(cron = "1 * * * * ?")
		public void checkMessageTop(){
			List<MessageInfo> messageInfos = messageTopServiceImpl.overdueTopList();
			for ( MessageInfo messageInfo: messageInfos
				 ) {
				//改状态
				messageTopServiceImpl.leaveTopById(messageInfo.getId());
				//发消息
				MessageRefund message=new MessageRefund();
				if(messageInfo.getMember() == null || messageInfo.getMember().getId() == null){
					continue;
				}
				message.setMemberId(messageInfo.getMember().getId());
				message.setMessageId(messageInfo.getId());
				message.setStatus(0);
				message.setType(NoticeTypeConst.UNDATE);
				message.setContent("您发布的消息"+ (StringUtils.isEmpty(messageInfo.getTitle()) ? "":messageInfo.getTitle()) +"已不再置顶，需要置顶请及时充值~");
				message.setCreateDate(new Date());
				message.setUpdateDate(new Date());
				messageRefundServiceImpl.saveMessageRefund(message);
			}
		}

}